﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using HYRevitCode.FormUtility;

namespace YArchitech.HVAC
{
	public partial class FrmDimension : System.Windows.Forms.Form
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		public FrmDimension(bool isAirTerminal)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			List<KeyValuePair<System.Windows.Forms.Control, bool>> inputControlGroup = new List<KeyValuePair<System.Windows.Forms.Control, bool>>();
			this.m_thisManger = new ControlInfoManager(this, inputControlGroup, "{70B8CC49-103C-4556-91A9-4A4184372CDA}", null);
			this.userSetting = new HVACUserSetting();
			this.isAirTerminal = isAirTerminal;
			base.Icon = Resource.YArchitechIcon;
			this.ReadData();
		}

		private void ReadData()
		{
			string parentName = this.isAirTerminal ? "AirTerminalDimension" : "SprinklerDimension";
			if (!(this.userSetting.ReadValue(parentName, "rbWall") == "") && Convert.ToBoolean(this.userSetting.ReadValue(parentName, "rbWall")))
			{
				this.rbWall.Checked = true;
				this.txtDistance.Enabled = false;
			}
			else
			{
				this.rbGrid.Checked = true;
				this.txtDistance.Enabled = true;
			}
			this.txtDistance.Text = ((this.userSetting.ReadValue(parentName, "txtDistance") == "") ? "0" : this.userSetting.ReadValue(parentName, "txtDistance"));
		}

		public bool SaveData()
		{
			string parentName = this.isAirTerminal ? "AirTerminalDimension" : "SprinklerDimension";
			this.userSetting.WriteValue(parentName, "txtDistance", this.txtDistance.Text);
			this.userSetting.WriteValue(parentName, "rbWall", this.rbWall.Checked.ToString());
			return true;
		}

		private void rbWall_CheckedChanged(object sender, EventArgs e)
		{
			if (((RadioButton)sender).Checked)
			{
				this.txtDistance.Enabled = false;
				return;
			}
			this.txtDistance.Enabled = true;
		}

		private void FrmDimension_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private void FrmDimension_FormClosed(object sender, FormClosedEventArgs e)
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			if (this.isAirTerminal)
			{
				FrmDimension.SendMessage(AirTerminalDimension.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			}
			else
			{
				FrmDimension.SendMessage(SprinklerDimension.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			}
			FrmDimension.keybd_event((byte)bVk, 0, dwFlags, 0);
			FrmDimension.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void FrmDimension_MouseLeave(object sender, EventArgs e)
		{
		}

		private void FrmDimension_Load(object sender, EventArgs e)
		{
			if (this.m_thisManger != null && !this.m_thisManger.SetControlInfoToForm())
			{
				base.Location = new System.Drawing.Point(230, 170);
			}
		}

		private void FrmDimension_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.m_thisManger != null)
			{
				this.m_thisManger.GetControlInfoToXml();
			}
		}

		private HVACUserSetting userSetting;

		private bool isAirTerminal = true;

		private ControlInfoManager m_thisManger;
	}
}
